package com.service.lx;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.luo.CusDaoM;
import com.dao.lx.ComplainDAO;
import com.dao.lx.ComplainDAOMybatis;
import com.dao.lx.UserDAO;
import com.entity.Complain;
import com.entity.Customer;
import com.entity.User;
import com.service.ServiceException;
import com.utils.Pager;

@Service
@Transactional(rollbackFor = Exception.class)
public class ComplainService {
	@Autowired
	private UserDAO userDao;
	@Autowired
	private CusDaoM cusDaoM;
	@Autowired
	private ComplainDAO complainDao;
	@Autowired
	private ComplainDAOMybatis complainDaoM;
	/**
	 * 添加/修改/逻辑删除
	 * @param complain
	 * @throws ServiceException
	 */
	public void addAndUpdateComplain(Complain complain) throws ServiceException{
		try {
			if(complain.getCusno()!=null){
				Customer cus=new Customer();
				cus.setCusno(complain.getCusno());
				List<Customer> customer=cusDaoM.selectCus(cus);
				if(customer.size()!=0){
					complain.setCustomer(customer.get(0));
				}else{
					complain.setCustomer(null);
				}
			}
			Complain complain2=complainDao.findOne(complain.getComplainId());
			if(complain2==null){
				complain.setComplainIfdelete(new BigDecimal(1));
			}
			if(complain.getUserId1()!=null){
				User user=userDao.findOne(complain.getUserId1());
				if(user!=null){
					complain.setUser(user);
				}else{
					complain.setUser(null);
				}
			}
			complain.setComplainIntodate(new Date());
			complainDao.save(complain);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param complainId
	 * @throws ServiceException
	 */
	public void deleteComplain(Long complainId) throws ServiceException{
		try {
			complainDao.delete(complainId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 逻辑删除
	 * @param complainId
	 */
	public void UpdateComplainIfdelete(Long complainId) {
		Complain complain = complainDao.findOne(complainId);
		complain.setComplainIfdelete(new BigDecimal(0));
		addAndUpdateComplain(complain);
	}
	
	/**
	 * 批量删除
	 * @param complainIds
	 */
	public void deletArrIfDelete(List<Long> complainIds) {
		if(complainIds != null) {
			for(Long complainId: complainIds) {
				Complain complain = complainDao.findOne(complainId);
				complain.setComplainIfdelete(new BigDecimal(0));
				addAndUpdateComplain(complain);
			}
		}
	}
	
	/**
	 * 单条查询
	 * @param complainId
	 * @return
	 */
	public Complain findOne(Long complainId) {
		return complainDao.findOne(complainId);
	}

	/**
	 * 快速查询：分类
	 * @param complainClassify
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Complain> findComplainClassify(String complainClassify, Integer curpage, Integer pagesize) {
		return complainDao.queryByComplainClassify(complainClassify, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：紧急程度
	 * @param complainUrgency
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Complain> findComplainUrgency(BigDecimal complainUrgency, Integer curpage, Integer pagesize) {
		return complainDao.queryByComplainUrgency(complainUrgency, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：处理结果
	 * @param complainResults
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Complain> findComplainResults(String complainResults, Integer curpage, Integer pagesize) {
		return complainDao.queryByComplainResults(complainResults, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：投诉主题
	 * @param complainDas
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Complain> findComplainDas(String complainDas, Integer curpage, Integer pagesize) {
		return complainDao.queryByComplainDas("%"+complainDas+"%", new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 查询所有
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Complain> findAllComplains(Integer curpage,Integer pagesize){
		return complainDao.findAllComplainIfdelete1(new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询（全部数据，分类，紧急程度，处理结果，投诉主题）
	 * @param field
	 * @param pagesize
	 * @return
	 */
	public Page<Complain> findFastAllComplains(String field, Integer curpage, Integer pagesize){
		String str;
		if(field.startsWith("complainClassify")){
			str=field.replace("complainClassify", "");
			return findComplainClassify(str, curpage, pagesize);
		}else if(field.startsWith("complainUrgency")){
			str=field.replace("complainUrgency", "");
			return findComplainUrgency(new BigDecimal(str), curpage, pagesize);
		}else if(field.startsWith("complainResults")){
			str=field.replace("complainResults", "");
			return findComplainResults(str, curpage, pagesize);
		}else if(field.startsWith("complainDas")){
			str=field.replace("complainDas", "");
			return findComplainDas(str, curpage, pagesize);
		}else{
			return findAllComplains(curpage, pagesize);
		}
	}
	
	/**
	 * 高级查询
	 * @param map
	 * @return
	 * @throws ParseException 
	 */
	public Pager<Complain> findHighAllComplains(Map map){
		Complain complain = (Complain) JSONObject.parseObject(map.get("list").toString(), Complain.class);
		Integer curpage=(Integer)map.get("curpage");
		Integer pagesize=(Integer)map.get("pagesize");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(complain.getComplainDateStartAndStop()!=null){
    		try {
    			complain.setComplainDateStart(sdf.parse((String) complain.getComplainDateStartAndStop().get(0)));
    			complain.setComplainDateStop(sdf.parse((String) complain.getComplainDateStartAndStop().get(1)));
    			for(int i=0;i<complain.getComplainDateStartAndStop().size();i++){
    				System.out.println(complain.getComplainDateStartAndStop().get(i));
    			}
    			System.out.println(complain.getComplainDateStart());
    			System.out.println(complain.getComplainDateStop());
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
        }
		List<Complain> list=(List<Complain>) complainDaoM.findTrim(complain);
		Pager<Complain> pager=new Pager<Complain>();
		if(list.size()!=0){
			list.get(list.size()-1);
			Integer start=(curpage-1)*pagesize;
			Integer stop=curpage*pagesize;
			if(list.size()<stop){
				stop=list.size();
			}
			if(list.size()<start){
				start=stop-list.size()%pagesize;
			}
			pager.setList(list.subList(start, stop));
			pager.setTotal(list.size());
		}else{
			pager.setList(new ArrayList());
			pager.setTotal(0);
		}
		return pager;
	}
}
